﻿using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList.Nodes;
using HuaCheng.CommonClass;
using HuaCheng.DataAccess;
using HuaCheng.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace HuaCheng.Views
{
    /// <summary>
    /// 通道数据细节显示
    /// </summary>
    public partial class ChannelDataDetial : Form
    {
        //测试id
        public string testId;
        //柜号
        public int gid;
        //通道号
        public string cid;
        //中位机号
        public int mid;
        //表名
        public string tabName;
        //独立或者并联模式
        public string mode;
        //自动生成内容的格子
        RepositoryItem TextNoEdit = new RepositoryItemTextEdit();
        //缓存子节点数据
        public Dictionary<TreeListNode, List<ChannelState>> subNodes = new Dictionary<TreeListNode, List<ChannelState>>();
        
        public ChannelDataDetial(string testId,int gid,int mid,string cid,string tableName,string mode)
        {
            InitializeComponent();
            this.testId = testId;
            this.gid = gid;
            this.cid = cid;
            this.mid = mid;
            this.tabName = tableName;
            this.mode = mode;
            TextNoEdit.ReadOnly = true;
            TextNoEdit.MouseDown += tLChannelData_MouseDown;
            
           // MessageBox.Show("加载中，请稍候。。。");
        }
        /// <summary>
        /// 初始化树形图
        /// </summary>
        /// <param name="testId"></param>
        /// <param name="gid"></param>
        /// <param name="cid"></param>
        /// <param name="tableName"></param>
        public void InitTreeList(string testId,int gid,int mid,string cid,string tableName,string mode)
        {
            if (mode == "1")
            {
                cid = ChannelDataHelper.IsParallel(testId, tableName);
            }      
            //查询所有执行的工步

            List<StepDetial> stepDetial = ChannelDataHelper.RetriveStepDetial(testId, gid,mid, cid, tableName);

            //查询工步名称和工步类型
            Dictionary<int, string> typeAndName = DicHelper.RetrieveStepNameType();
            try
            {
                if (stepDetial.Count > 0 && typeAndName.Count > 0)
                {
                    int stepNum = stepDetial[0].StepNum;
                    TreeListNode firNode = tLChannelData.Nodes.Add();//这一行保留为每个工步号的汇总数据
                    tLChannelData.FocusedNode = firNode;
                    tLChannelData.Nodes[0].SetValue(0, stepDetial[0].StepNum);//工步序号
                    int n = 1;
                    for (int i = 0; i < stepDetial.Count; i++)
                    {
                        if (stepNum != stepDetial[i].StepNum)//如果工步号不等于上一条记录的工步号则另起一行
                        {
                            firNode = tLChannelData.Nodes.Add();
                            tLChannelData.FocusedNode = firNode;
                            firNode.SetValue(0, stepDetial[i].StepNum);//实际工步号
                            n = 1;//从头开始计数
                        }
                        //汇总数据
                        TreeListNode secNode = firNode.Nodes.Add();//这一行显示每个工步的汇总数据                        
                        tLChannelData.FocusedNode = secNode;
                        secNode.SetValue(0, n);//执行时每个工步的执行序号
                        secNode.SetValue(1, typeAndName[stepDetial[i].StepType]);
                        //每条汇总数据下面的子数据
                        List<ChannelState> channelData = ChannelDataHelper.RetriveChannelData(testId, mid,gid, cid,stepDetial[i].startId,stepDetial[i].DetialCount,tableName);
                        //加入到缓存
                        subNodes.Add(secNode, channelData);
                        //for (int j = 0; j < channelData.Count; j++)
                        //{
                        //    TreeListNode thirdNode = secNode.Nodes.Add();
                        //    tLChannelData.FocusedNode = thirdNode;
                        //    thirdNode.SetValue(0, j + 1);//工步记录序号
                        //    TimeSpan ts = new TimeSpan(0, 0, 0, 0, channelData[j].TestTime);
                        //    thirdNode.SetValue(1, ts.ToString());//记录时间
                        //    thirdNode.SetValue(2, channelData[j].DC_outputVolt);//DC输出电压
                        //    thirdNode.SetValue(3, channelData[j].DC_loadCurrent);//DC负载电流
                        //}

                        stepNum = stepDetial[i].StepNum;//记录上一条工步序号
                        n++;
                        secNode.Collapse();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }

        }
        /// <summary>
        /// 切换节点时显示不同的标题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tLChannelData_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            TreeListNode tn = tLChannelData.FocusedNode;
            if (tn.Level == 0)
            {
                this.tLChannelData.Columns[0].Caption = ResourceCulture.GetString("StepID");//"工步序号"
                this.tLChannelData.Columns[1].Caption = ResourceCulture.GetString("TotalCapacity");// "总容量";
                this.tLChannelData.Columns[2].Caption = ResourceCulture.GetString("TotalEnergy"); //"总能量";
                this.tLChannelData.Columns[3].Caption = ResourceCulture.GetString("Voltageamplitude"); //"电压幅值";
                this.tLChannelData.Columns[4].Caption = ResourceCulture.GetString("Currentamplitude"); //"电流幅值";
                this.tLChannelData.Columns[5].Caption = " ";
                this.tLChannelData.Columns[6].Caption = " ";
                this.tLChannelData.Columns[7].Caption = " ";
                this.tLChannelData.Columns[8].Caption = " ";
                this.tLChannelData.Columns[9].Caption = " ";
                this.tLChannelData.Columns[10].Caption = " ";
                this.tLChannelData.Columns[11].Caption = " ";
                this.tLChannelData.Columns[12].Caption = " ";
                this.tLChannelData.Columns[13].Caption = " ";
            }
            else if(tn.Level==1)
            {
                this.tLChannelData.Columns[0].Caption = "执行序号";
                this.tLChannelData.Columns[1].Caption = "充电容量(Ah)";
                this.tLChannelData.Columns[2].Caption = "放电容量(Ah)";
                this.tLChannelData.Columns[3].Caption = "充放电效率(%)";
                this.tLChannelData.Columns[4].Caption = "放电能量(Wh)";
                this.tLChannelData.Columns[5].Caption = "充电能量(Wh)";
                this.tLChannelData.Columns[6].Caption = " ";
                this.tLChannelData.Columns[7].Caption = " ";
                this.tLChannelData.Columns[8].Caption = " ";
                this.tLChannelData.Columns[9].Caption = " ";
                this.tLChannelData.Columns[10].Caption = " ";
                this.tLChannelData.Columns[11].Caption = " ";
                this.tLChannelData.Columns[12].Caption = " ";
                this.tLChannelData.Columns[13].Caption = " ";
            }
            else 
            {
                this.tLChannelData.Columns[0].Caption = ResourceCulture.GetString("RecordID");//"记录序号";
                this.tLChannelData.Columns[1].Caption = "记录时间";
                this.tLChannelData.Columns[2].Caption = "电压";
                this.tLChannelData.Columns[3].Caption = "电流";
                this.tLChannelData.Columns[4].Caption = "容量";
                this.tLChannelData.Columns[5].Caption = "能量";
                this.tLChannelData.Columns[6].Caption = "绝对时间";
                this.tLChannelData.Columns[7].Caption = " ";
                this.tLChannelData.Columns[8].Caption = " ";
                this.tLChannelData.Columns[9].Caption = " ";
                this.tLChannelData.Columns[10].Caption = " ";
                this.tLChannelData.Columns[11].Caption = " ";
                this.tLChannelData.Columns[12].Caption = " ";
                this.tLChannelData.Columns[13].Caption = " ";
            }
        }
        /// <summary>
        /// 在页面没有加载完成前提示用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChannelDataDetial_Shown(object sender, EventArgs e)
        {
            FrmWaiting frm = new FrmWaiting("数据正在加载中...");
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Show();
            Application.DoEvents();
            InitTreeList(testId, gid,mid, cid, tabName, mode);//初始化树形图显示工步详细信息
            frm.Close();
        }
        /// <summary>
        /// 双击汇总数据节点动态添加数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tLChannelData_DoubleClick(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 设置所有单元格属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tLChannelData_CustomNodeCellEdit(object sender, DevExpress.XtraTreeList.GetCustomNodeCellEditEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                e.RepositoryItem = TextNoEdit;
            }           
        }

        private void tLChannelData_MouseDown(object sender, MouseEventArgs e)
        {
            //获取当前点击的节点
            TreeListNode clickedNode = this.tLChannelData.FocusedNode;
            if (subNodes.ContainsKey(clickedNode))
            {
                List<ChannelState> channelData = subNodes[clickedNode];
                if (clickedNode.Nodes.Count < channelData.Count)
                {
                    for (int j = 0; j < channelData.Count; j++)
                    {
                        TreeListNode thirdNode = clickedNode.Nodes.Add();
                        tLChannelData.FocusedNode = thirdNode;
                        thirdNode.SetValue(0, j + 1);//工步记录序号
                        TimeSpan ts = new TimeSpan(0, 0, 0, 0, channelData[j].TestTime);
                        thirdNode.SetValue(1, ts.ToString());//记录时间
                        thirdNode.SetValue(2, channelData[j].DC_outputVolt);//DC输出电压
                        thirdNode.SetValue(3, channelData[j].DC_loadCurrent);//DC负载电流
                    }
                }
            }
        }
        #region 国际化
        private void ChannelDataDetial_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("ChannelData");
        }
        #endregion
    }
}
